public class Solution {
    public String shortestSuperstring(String[] ws) {
        int n = ws.length, mask = 1 << n;
        int[][] g = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                String a = ws[i], b = ws[j];
                int l1 = a.length(), l2 = b.length(), len = Math.min(l1, l2);
                for (int k = len; k >= 1; k--) {
                    if (a.substring(l1 - k).equals(b.substring(0, k))) {
                        g[i][j] = k;
                        break;
                    }
                }
            }
        }
        int[][] f = new int[mask][n], p = new int[mask][n];
        for (int s = 0; s < mask; s++) {
            for (int i = 0; i < n; i++) {
                if (((s >> i) & 1) == 0) continue;
                for (int j = 0; j < n; j++) {
                    if (((s >> j) & 1) == 1) continue;
                    if (f[s | (1 << j)][j] <= f[s][i] + g[i][j]) {
                        f[s | (1 << j)][j] = f[s][i] + g[i][j];
                        p[s | (1 << j)][j] = i;
                    }
                }
            }
        }
        int max = f[mask - 1][0], idx = 0, last = -1, status = mask - 1;
        for (int i = 1; i < n; i++) {
            if (max < f[mask - 1][i]) {
                max = f[mask - 1][i];
                idx = i;
            }
        }
        String ans = "";
        while (status != 0) {
            if (last == -1) ans = ws[idx];
            else ans = ws[idx].substring(0, ws[idx].length() - g[idx][last]) + ans;
            last = idx;
            idx = p[status][idx];
            status ^= (1 << last);
        }
        return ans;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        String[] ws = {"catg", "ctaagt", "gcta", "ttca", "atgcatc"};
        String result = solution.shortestSuperstring(ws);
        System.out.println(result);
    }
}
